import { ref } from 'vue';
import { chargeItemStandards, queryChargeItemList } from '@/api/finance/basic/common';
import { message } from 'ant-design-vue';
import { isNotEmpty, isArray } from '@/utils/is';

const useChargeItem = (modelRef: any, chargeTypeCodes = []) => {
    const chargeItemTypeTree = ref<any[]>([]);
    const chargeStandardList = ref<any[]>([]);

    const queryChargeItem = () => {
        queryChargeItemList(chargeTypeCodes).then(res => {
            chargeItemTypeTree.value = res.data;
        });
    };
    const handleSearchTable = (value, option) => {
        console.log(value, option);
        delete modelRef.chargeStandardId;
        delete modelRef.chargeStandard;
        modelRef.chargeItem = option.name;

        const { chargeItemIds, projectCode } = modelRef;
        if (!isNotEmpty(projectCode)) {
            message.warning('请选择归属项目');
            return false;
        }
        if (!isNotEmpty(chargeItemIds)) {
            message.warning('请选择收费科目');
            return false;
        }

        chargeItemStandards({
            chargeItemIds,
            projectCode: modelRef.projectCode.at(-1),
        }).then(res => {
            console.log(res);
            chargeStandardList.value = (res.data || []).reduce(
                (pre, cur) => [
                    ...pre,
                    ...(cur.standards || []).map(item => ({
                        ...item,
                        value: item.chargeStandardId,
                        label: `${item.chargeStandard}`,
                        chargeUnit: cur.chargeUnit,
                        chargeItemId: cur.chargeItemId,
                    })),
                ],
                [],
            );
            if (modelRef.chargeStandardId && !chargeStandardList.value.some(item => item.value === modelRef.chargeStandardId)) {
                modelRef.chargeStandardId = undefined;
                console.log(modelRef);
            }
        });
    };

    const handleChargeItemSelect = (value, option) => {
        modelRef.chargeItem = option.name;
    };

    const handleChargeItemSelectSearch = (value, option) => {
        console.log(value, option);
        if (isNotEmpty(option)) {
            modelRef.chargeItem = option.name;
            delete modelRef.chargeStandardId;
            delete modelRef.chargeStandard;
        }

        const { projectCode } = modelRef;
        if (!isNotEmpty(projectCode)) {
            message.warning('请选择归属项目');
            return false;
        }

        chargeItemStandards({
            chargeItemIds: [value],
            projectCode: isArray(modelRef.projectCode) ? modelRef.projectCode.at(-1) : modelRef.projectCode,
        }).then(res => {
            console.log(res);
            chargeStandardList.value = (res.data || []).reduce(
                (pre, cur) => [
                    ...pre,
                    ...(cur.standards || []).map(item => ({
                        ...item,
                        value: item.chargeStandardId,
                        label: `${item.chargeStandard}`,
                        chargeUnit: cur.chargeUnit,
                        chargeItemId: cur.chargeItemId,
                    })),
                ],
                [],
            );
            if (modelRef.chargeStandardId && !chargeStandardList.value.some(item => item.value === modelRef.chargeStandardId)) {
                modelRef.chargeStandardId = undefined;
                console.log(modelRef);
            }
        });
    };

    const filterOption = (input: string, option: any) => {
        return option.value.toLowerCase().indexOf(input.toLowerCase()) >= 0;
    };

    return {
        chargeItemTypeTree,
        chargeStandardList,

        filterOption,

        queryChargeItem,
        handleSearchTable,
        handleChargeItemSelect,
        handleChargeItemSelectSearch,
    };
};

export default useChargeItem;
